/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.ignoramus.application.view.rcp;

import java.io.File;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.ide.IDE;

/**
 * Standard action for opening an editor on local file(s).
 * <p>
 * This class may be instantiated; it is not intended to be subclassed.
 * </p>
 */
public class OpenLocalFileAction extends Action implements IWorkbenchAction
{

	private IWorkbenchWindow	window;
	private String				filterPath;

	/**
	 * Creates a new action for opening a local file.
	 */
	public OpenLocalFileAction(String label, IWorkbenchWindow	window)
	{
		this.window =  window;
		filterPath =  System.getProperty("user.home"); //$NON-NLS-1$
		setText(label);
        // The id is used to refer to the action in a menu or toolbar
		setId(ICommandIds.CMD_OPEN_FILE);
        // Associate the action with a pre-defined command, to allow key bindings.
		setActionDefinitionId(ICommandIds.CMD_OPEN_FILE);
		setImageDescriptor(Activator.getImageDescriptor("/icons/sample2.gif"));
		setEnabled(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.action.Action#run()
	 */
	public void run()
	{
		FileDialog dialog = new FileDialog(window.getShell(), SWT.OPEN | SWT.MULTI);
		dialog.setText("Open local file");
		String[] filterExtensions = {"*.ign", "*,*"};
		dialog.setFilterExtensions(filterExtensions);
		String[] filterNames = {"I-gnoramus files", "All files"};
		dialog.setFilterNames(filterNames);
		dialog.setFilterPath(filterPath);
		dialog.open();
		String[] names = dialog.getFileNames();
		
		
		for (int i = 0; i < names.length; i++)
		{

			File fileToOpen = new File(filterPath + "/" + names[i]);
			 
			if (fileToOpen.exists() && fileToOpen.isFile()) {
			    IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileToOpen.toURI());
			    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
			 
			    try {
			        IDE.openEditorOnFileStore( page, fileStore );
			    } catch ( PartInitException e ) {
			    	e.printStackTrace();
			    }
			} else {
			    //Do something if the file does not exist
			}
		}
		
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
	 */
	public void dispose() {
		window =  null;
		filterPath =  null;
	}
}
